package com.lottery.ruok.lottrey.activity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.ui.widget.CustomDatePicker;
import com.lottery.ruok.lottrey.ui.widget.DatePickerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 选择日期
* */

public class ChooseDateActivity extends BaseActivity {
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.day_txt)
    TextView day_txt;
    @BindView(R.id.day_layout)
    FrameLayout day_layout;

    private Calendar nowCalendar = Calendar.getInstance();
    private int type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_date);

        ButterKnife.bind(this);

        init();
    }

    private void init() {
        type = getIntent().getIntExtra("type",0);
        getDate();
    }

    /**
     * 开始时间选择器的逻辑
     */
    private void getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        if(getIntent().getStringExtra("time") != null){
            now = getIntent().getStringExtra("time");
        }

        try {
            nowCalendar.setTime(sdf.parse(now));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar startCal = Calendar.getInstance();
        startCal.add(Calendar.YEAR, -10);
        Date minDate = startCal.getTime();

        Calendar endCal = Calendar.getInstance();
        endCal.add(Calendar.YEAR, 10);
        Date maxDate = endCal.getTime();

        String min;
        if(getIntent().getStringExtra("startTime") == null){
            min = sdf.format(minDate);
        } else {
            min = getIntent().getStringExtra("startTime");
        }
        String max;
        if(getIntent().getStringExtra("endTime") == null){
            max = sdf.format(maxDate);
        } else {
            max = getIntent().getStringExtra("endTime");
        }

        if (isValidDate(min, "yyyy-MM-dd HH:mm") && isValidDate(max, "yyyy-MM-dd HH:mm")) {
            canAccess = true;
            selectedCalender = Calendar.getInstance();
            startCalendar = Calendar.getInstance();
            endCalendar = Calendar.getInstance();
            try {
                startCalendar.setTime(sdf.parse(min));
                endCalendar.setTime(sdf.parse(max));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            initView();
        }
        showDayTime(type!=3);
        showSpecificTime(false); // 不显示时和分
        setIsLoop(false); // 不允许循环滚动
        setMonthClear(true);
        setDayClear(true);
        setHourClear(true);
        setMinClear(true);
        show(now);
    }

    private int scrollUnits = CustomDatePicker.SCROLL_TYPE.HOUR.value + CustomDatePicker.SCROLL_TYPE.MINUTE.value;
    private boolean canAccess = false;

    private DatePickerView year_pv, month_pv, day_pv, hour_pv, minute_pv;

    private static final int MAX_MINUTE = 59;
    private static final int MAX_HOUR = 23;
    private static final int MIN_MINUTE = 0;
    private static final int MIN_HOUR = 0;
    private static final int MAX_MONTH = 12;

    private ArrayList<String> year, month, day, hour, minute;
    private int startYear, startMonth, startDay, startHour, startMinute, endYear, endMonth, endDay, endHour, endMinute;
    private boolean spanYear, spanMon, spanDay, spanHour, spanMin;
    private Calendar selectedCalender, startCalendar, endCalendar;
    private TextView hour_text, minute_text;
    private boolean monthClear, dayClear, hourClear, minClear;

    private void initView() {
        year_pv = findViewById(R.id.year_pv);
        month_pv = findViewById(R.id.month_pv);
        day_pv = findViewById(R.id.day_pv);
        hour_pv = findViewById(R.id.hour_pv);
        minute_pv = findViewById(R.id.minute_pv);
        hour_text = findViewById(R.id.hour_text);
        minute_text = findViewById(R.id.minute_text);
    }

    private void initParameter() {
        startYear = startCalendar.get(Calendar.YEAR);
        startMonth = startCalendar.get(Calendar.MONTH) + 1;
        startDay = startCalendar.get(Calendar.DAY_OF_MONTH);
        startHour = startCalendar.get(Calendar.HOUR_OF_DAY);
        startMinute = startCalendar.get(Calendar.MINUTE);
        endYear = endCalendar.get(Calendar.YEAR);
        endMonth = endCalendar.get(Calendar.MONTH) + 1;
        endDay = endCalendar.get(Calendar.DAY_OF_MONTH);
        endHour = endCalendar.get(Calendar.HOUR_OF_DAY);
        endMinute = endCalendar.get(Calendar.MINUTE);
        spanYear = startYear != endYear;
        spanMon = (!spanYear) && (startMonth != endMonth);
        spanDay = (!spanMon) && (startDay != endDay);
        spanHour = (!spanDay) && (startHour != endHour);
        spanMin = (!spanHour) && (startMinute != endMinute);
        selectedCalender.setTime(startCalendar.getTime());
    }

    private void initTimer() {
        initArrayList();
        if (spanYear) {
            for (int i = startYear; i <= endYear; i++) {
                year.add(String.valueOf(i));
            }
            for (int i = startMonth; i <= MAX_MONTH; i++) {
                month.add(formatTimeUnit(i));
            }
            for (int i = startDay; i <= startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
                day.add(formatTimeUnit(i));
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) != CustomDatePicker.SCROLL_TYPE.HOUR.value) {
                hour.add(formatTimeUnit(startHour));
            } else {
                for (int i = startHour; i <= MAX_HOUR; i++) {
                    hour.add(formatTimeUnit(i));
                }
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) != CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
                minute.add(formatTimeUnit(startMinute));
            } else {
                for (int i = startMinute; i <= MAX_MINUTE; i++) {
                    minute.add(formatTimeUnit(i));
                }
            }
        } else if (spanMon) {
            year.add(String.valueOf(startYear));
            for (int i = startMonth; i <= endMonth; i++) {
                month.add(formatTimeUnit(i));
            }
            for (int i = startDay; i <= startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
                day.add(formatTimeUnit(i));
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) != CustomDatePicker.SCROLL_TYPE.HOUR.value) {
                hour.add(formatTimeUnit(startHour));
            } else {
                for (int i = startHour; i <= MAX_HOUR; i++) {
                    hour.add(formatTimeUnit(i));
                }
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) != CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
                minute.add(formatTimeUnit(startMinute));
            } else {
                for (int i = startMinute; i <= MAX_MINUTE; i++) {
                    minute.add(formatTimeUnit(i));
                }
            }
        } else if (spanDay) {
            year.add(String.valueOf(startYear));
            month.add(formatTimeUnit(startMonth));
            for (int i = startDay; i <= endDay; i++) {
                day.add(formatTimeUnit(i));
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) != CustomDatePicker.SCROLL_TYPE.HOUR.value) {
                hour.add(formatTimeUnit(startHour));
            } else {
                for (int i = startHour; i <= MAX_HOUR; i++) {
                    hour.add(formatTimeUnit(i));
                }
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) != CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
                minute.add(formatTimeUnit(startMinute));
            } else {
                for (int i = startMinute; i <= MAX_MINUTE; i++) {
                    minute.add(formatTimeUnit(i));
                }
            }
        } else if (spanHour) {
            year.add(String.valueOf(startYear));
            month.add(formatTimeUnit(startMonth));
            day.add(formatTimeUnit(startDay));

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) != CustomDatePicker.SCROLL_TYPE.HOUR.value) {
                hour.add(formatTimeUnit(startHour));
            } else {
                for (int i = startHour; i <= endHour; i++) {
                    hour.add(formatTimeUnit(i));
                }
            }

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) != CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
                minute.add(formatTimeUnit(startMinute));
            } else {
                for (int i = startMinute; i <= MAX_MINUTE; i++) {
                    minute.add(formatTimeUnit(i));
                }
            }
        } else if (spanMin) {
            year.add(String.valueOf(startYear));
            month.add(formatTimeUnit(startMonth));
            day.add(formatTimeUnit(startDay));
            hour.add(formatTimeUnit(startHour));

            if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) != CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
                minute.add(formatTimeUnit(startMinute));
            } else {
                for (int i = startMinute; i <= endMinute; i++) {
                    minute.add(formatTimeUnit(i));
                }
            }
        }
        loadComponent();
    }

    /**
     * 将“0-9”转换为“00-09”
     */
    private String formatTimeUnit(int unit) {
        return unit < 10 ? "0" + String.valueOf(unit) : String.valueOf(unit);
    }

    private void initArrayList() {
        if (year == null) year = new ArrayList<>();
        if (month == null) month = new ArrayList<>();
        if (day == null) day = new ArrayList<>();
        if (hour == null) hour = new ArrayList<>();
        if (minute == null) minute = new ArrayList<>();
        year.clear();
        month.clear();
        day.clear();
        hour.clear();
        minute.clear();
    }

    private void loadComponent() {
        year_pv.setData(year);
        month_pv.setData(month);
        day_pv.setData(day);
        hour_pv.setData(hour);
        minute_pv.setData(minute);
        year_pv.setSelected(0);
        month_pv.setSelected(0);
        day_pv.setSelected(0);
        hour_pv.setSelected(0);
        minute_pv.setSelected(0);
        executeScroll();
    }

    private void addListener() {
        year_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                selectedCalender.set(Calendar.YEAR, Integer.parseInt(text));
                monthChange();
            }
        });

        month_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                selectedCalender.set(Calendar.DAY_OF_MONTH, 1);
                selectedCalender.set(Calendar.MONTH, Integer.parseInt(text) - 1);
                dayChange();
            }
        });

        day_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                selectedCalender.set(Calendar.DAY_OF_MONTH, Integer.parseInt(text));
                hourChange();
            }
        });

        hour_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                selectedCalender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(text));
                minuteChange();
            }
        });

        minute_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                selectedCalender.set(Calendar.MINUTE, Integer.parseInt(text));
            }
        });
    }

    private void monthChange() {
        month.clear();
        int selectedYear = selectedCalender.get(Calendar.YEAR);
        if (selectedYear == startYear) {
            for (int i = startMonth; i <= MAX_MONTH; i++) {
                month.add(formatTimeUnit(i));
            }
        } else if (selectedYear == endYear) {
            for (int i = 1; i <= endMonth; i++) {
                month.add(formatTimeUnit(i));
            }
        } else {
            for (int i = 1; i <= MAX_MONTH; i++) {
                month.add(formatTimeUnit(i));
            }
        }
        selectedCalender.set(Calendar.MONTH, Integer.parseInt(month.get(0)) - 1);
        month_pv.setData(month);
        month_pv.setSelected(0);
        executeAnimator(month_pv);

        month_pv.postDelayed(new Runnable() {
            @Override
            public void run() {
                dayChange();
            }
        }, 100);
    }

    private void dayChange() {
        day.clear();
        int selectedYear = selectedCalender.get(Calendar.YEAR);
        int selectedMonth = selectedCalender.get(Calendar.MONTH) + 1;
        if (selectedYear == startYear && selectedMonth == startMonth) {
            for (int i = startDay; i <= selectedCalender.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
                day.add(formatTimeUnit(i));
            }
        } else if (selectedYear == endYear && selectedMonth == endMonth) {
            for (int i = 1; i <= endDay; i++) {
                day.add(formatTimeUnit(i));
            }
        } else {
            for (int i = 1; i <= selectedCalender.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
                day.add(formatTimeUnit(i));
            }
        }
        selectedCalender.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day.get(0)));
        day_pv.setData(day);
        day_pv.setSelected(0);
        executeAnimator(day_pv);

        day_pv.postDelayed(new Runnable() {
            @Override
            public void run() {
                hourChange();
            }
        }, 100);
    }

    private void hourChange() {
        if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) == CustomDatePicker.SCROLL_TYPE.HOUR.value) {
            hour.clear();
            int selectedYear = selectedCalender.get(Calendar.YEAR);
            int selectedMonth = selectedCalender.get(Calendar.MONTH) + 1;
            int selectedDay = selectedCalender.get(Calendar.DAY_OF_MONTH);
            if (selectedYear == startYear && selectedMonth == startMonth && selectedDay == startDay) {
                for (int i = startHour; i <= MAX_HOUR; i++) {
                    hour.add(formatTimeUnit(i));
                }
            } else if (selectedYear == endYear && selectedMonth == endMonth && selectedDay == endDay) {
                for (int i = MIN_HOUR; i <= endHour; i++) {
                    hour.add(formatTimeUnit(i));
                }
            } else {
                for (int i = MIN_HOUR; i <= MAX_HOUR; i++) {
                    hour.add(formatTimeUnit(i));
                }
            }
            selectedCalender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour.get(0)));
            hour_pv.setData(hour);
            hour_pv.setSelected(0);
            executeAnimator(hour_pv);
        }

        hour_pv.postDelayed(new Runnable() {
            @Override
            public void run() {
                minuteChange();
            }
        }, 100);
    }

    private void minuteChange() {
        if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) == CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
            minute.clear();
            int selectedYear = selectedCalender.get(Calendar.YEAR);
            int selectedMonth = selectedCalender.get(Calendar.MONTH) + 1;
            int selectedDay = selectedCalender.get(Calendar.DAY_OF_MONTH);
            int selectedHour = selectedCalender.get(Calendar.HOUR_OF_DAY);
            if (selectedYear == startYear && selectedMonth == startMonth && selectedDay == startDay && selectedHour == startHour) {
                for (int i = startMinute; i <= MAX_MINUTE; i++) {
                    minute.add(formatTimeUnit(i));
                }
            } else if (selectedYear == endYear && selectedMonth == endMonth && selectedDay == endDay && selectedHour == endHour) {
                for (int i = MIN_MINUTE; i <= endMinute; i++) {
                    minute.add(formatTimeUnit(i));
                }
            } else {
                for (int i = MIN_MINUTE; i <= MAX_MINUTE; i++) {
                    minute.add(formatTimeUnit(i));
                }
            }
            selectedCalender.set(Calendar.MINUTE, Integer.parseInt(minute.get(0)));
            minute_pv.setData(minute);
            minute_pv.setSelected(0);
            executeAnimator(minute_pv);
        }
        executeScroll();
    }

    private void executeAnimator(View view) {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.3f, 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.3f, 1f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(200).start();
    }

    private void executeScroll() {
        year_pv.setCanScroll(year.size() > 1);
        month_pv.setCanScroll(month.size() > 1);
        day_pv.setCanScroll(day.size() > 1);
        hour_pv.setCanScroll(hour.size() > 1 && (scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) == CustomDatePicker.SCROLL_TYPE.HOUR.value);
        minute_pv.setCanScroll(minute.size() > 1 && (scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) == CustomDatePicker.SCROLL_TYPE.MINUTE.value);
    }

    private void disScrollUnit(CustomDatePicker.SCROLL_TYPE... scroll_types) {
        if (scroll_types == null || scroll_types.length == 0) {
            scrollUnits = CustomDatePicker.SCROLL_TYPE.HOUR.value + CustomDatePicker.SCROLL_TYPE.MINUTE.value;
        } else {
            for (CustomDatePicker.SCROLL_TYPE scroll_type : scroll_types) {
                scrollUnits ^= scroll_type.value;
            }
        }
    }

    public void show(String time) {
        if (canAccess) {
            if (isValidDate(time, "yyyy-MM-dd")) {
                if (startCalendar.getTime().getTime() < endCalendar.getTime().getTime()) {
                    canAccess = true;
                    initParameter();
                    initTimer();
                    addListener();
                    setSelectedTime(time);

                    //手动刷新一次，为了第一次进入时间显示正确

//                    selectedCalender.set(Calendar.YEAR, nowCalendar.get(Calendar.YEAR));
//                    monthChange();
//                    selectedCalender.set(Calendar.DAY_OF_MONTH, 1);
//                    selectedCalender.set(Calendar.MONTH, nowCalendar.get(Calendar.MONTH));
//                    dayChange();
//                    selectedCalender.set(Calendar.DAY_OF_MONTH, nowCalendar.get(Calendar.DAY_OF_MONTH));
//                    hourChange();
                }
            } else {
                canAccess = false;
            }
        }
    }

    /**
     * 设置日期控件是否显示天
     */
    public void showDayTime(boolean show) {
        if (canAccess) {
            if (show) {
                day_layout.setVisibility(View.VISIBLE);
                day_txt.setVisibility(View.VISIBLE);
            } else {
                day_layout.setVisibility(View.GONE);
                day_txt.setVisibility(View.GONE);
            }
        }
    }


    /**
     * 设置日期控件是否显示时和分
     */
    public void showSpecificTime(boolean show) {
        if (canAccess) {
            if (show) {
                disScrollUnit();
                hour_pv.setVisibility(View.VISIBLE);
                hour_text.setVisibility(View.VISIBLE);
                minute_pv.setVisibility(View.VISIBLE);
                minute_text.setVisibility(View.VISIBLE);
            } else {
                disScrollUnit(CustomDatePicker.SCROLL_TYPE.HOUR, CustomDatePicker.SCROLL_TYPE.MINUTE);
                hour_pv.setVisibility(View.GONE);
                hour_text.setVisibility(View.GONE);
                minute_pv.setVisibility(View.GONE);
                minute_text.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置日期控件是否可以循环滚动
     */
    public void setIsLoop(boolean isLoop) {
        if (canAccess) {
            this.year_pv.setIsLoop(isLoop);
            this.month_pv.setIsLoop(isLoop);
            this.day_pv.setIsLoop(isLoop);
            this.hour_pv.setIsLoop(isLoop);
            this.minute_pv.setIsLoop(isLoop);
        }
    }

    /**
     * 设置日期控件默认选中的时间
     */
    public void setSelectedTime(String time) {
        if (canAccess) {
            String[] str = time.split(" ");
            String[] dateStr = str[0].split("-");//年月日

            /*int selectedYear = selectedCalender.get(Calendar.YEAR);
            int selectedMonth = selectedCalender.get(Calendar.MONTH) + 1;*/

            year_pv.setSelected(dateStr[0]);//年
            selectedCalender.set(Calendar.YEAR, Integer.parseInt(dateStr[0]));


            if (monthClear) {
                month.clear();
                if (nowCalendar.get(Calendar.YEAR) == startYear) {
                    for (int i = startMonth; i <= MAX_MONTH; i++) {
                        month.add(formatTimeUnit(i));
                    }
                } else if (nowCalendar.get(Calendar.YEAR) == endYear) {
                    for (int i = 1; i <= endMonth; i++) {
                        month.add(formatTimeUnit(i));
                    }
                } else {
                    for (int i = 1; i <= MAX_MONTH; i++) {
                        month.add(formatTimeUnit(i));
                    }
                }
//                for (int i = 1; i <= MAX_MONTH; i++) {
//                    month.add(formatTimeUnit(i));
//                }
            }
            month_pv.setData(month);
            month_pv.setSelected(dateStr[1]);
            selectedCalender.set(Calendar.MONTH, Integer.parseInt(dateStr[1]) - 1);
            executeAnimator(month_pv);


            if (dayClear) {
                day.clear();
                if (nowCalendar.get(Calendar.YEAR) == startYear && nowCalendar.get(Calendar.MONTH) + 1 == startMonth) {
                    for (int i = startDay; i <= selectedCalender.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
                        day.add(formatTimeUnit(i));
                    }
                } else if (nowCalendar.get(Calendar.YEAR) == endYear && nowCalendar.get(Calendar.MONTH) + 1 == endMonth) {
                    for (int i = 1; i <= endDay; i++) {
                        day.add(formatTimeUnit(i));
                    }
                } else {
                    for (int i = 1; i <= selectedCalender.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
                        day.add(formatTimeUnit(i));
                    }
                }
//                for (int i = 1; i <= selectedCalender.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
//                    day.add(formatTimeUnit(i));
//                }
            }
            day_pv.setData(day);
            day_pv.setSelected(dateStr[2]);
            selectedCalender.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr[2]));
            executeAnimator(day_pv);

            if (str.length == 2) {
                String[] timeStr = str[1].split(":");
                if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.HOUR.value) == CustomDatePicker.SCROLL_TYPE.HOUR.value) {
                    if (hourClear) {
                        hour.clear();
                        int selectedDay = selectedCalender.get(Calendar.DAY_OF_MONTH);
                        /*if (selectedYear == startYear && selectedMonth == startMonth && selectedDay == startDay) {
                            for (int i = startHour; i <= MAX_HOUR; i++) {
                                hour.add(formatTimeUnit(i));
                            }
                        } else if (selectedYear == endYear && selectedMonth == endMonth && selectedDay == endDay) {
                            for (int i = MIN_HOUR; i <= endHour; i++) {
                                hour.add(formatTimeUnit(i));
                            }
                        } else {
                            for (int i = MIN_HOUR; i <= MAX_HOUR; i++) {
                                hour.add(formatTimeUnit(i));
                            }
                        }*/
                        for (int i = MIN_HOUR; i <= MAX_HOUR; i++) {
                            hour.add(formatTimeUnit(i));
                        }
                    }
                    hour_pv.setData(hour);
                    hour_pv.setSelected(timeStr[0]);
                    selectedCalender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeStr[0]));
                    executeAnimator(hour_pv);
                }

                if ((scrollUnits & CustomDatePicker.SCROLL_TYPE.MINUTE.value) == CustomDatePicker.SCROLL_TYPE.MINUTE.value) {
                    if (minClear) {
                        minute.clear();
                        int selectedDay = selectedCalender.get(Calendar.DAY_OF_MONTH);
                        int selectedHour = selectedCalender.get(Calendar.HOUR_OF_DAY);
                        /*if (selectedYear == startYear && selectedMonth == startMonth && selectedDay == startDay && selectedHour == startHour) {
                            for (int i = startMinute; i <= MAX_MINUTE; i++) {
                                minute.add(formatTimeUnit(i));
                            }
                        } else if (selectedYear == endYear && selectedMonth == endMonth && selectedDay == endDay && selectedHour == endHour) {
                            for (int i = MIN_MINUTE; i <= endMinute; i++) {
                                minute.add(formatTimeUnit(i));
                            }
                        } else {
                            for (int i = MIN_MINUTE; i <= MAX_MINUTE; i++) {
                                minute.add(formatTimeUnit(i));
                            }
                        }*/
                        for (int i = MIN_MINUTE; i <= MAX_MINUTE; i++) {
                            minute.add(formatTimeUnit(i));
                        }
                    }
                    minute_pv.setData(minute);
                    minute_pv.setSelected(timeStr[1]);
                    selectedCalender.set(Calendar.MINUTE, Integer.parseInt(timeStr[1]));
                    executeAnimator(minute_pv);
                }

            }
            executeScroll();
        }
    }

    /**
     * 验证字符串是否是一个合法的日期格式
     */
    private boolean isValidDate(String date, String template) {
        boolean convertSuccess = true;
        // 指定日期格式
        SimpleDateFormat format = new SimpleDateFormat(template, Locale.CHINA);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2015/02/29会被接受，并转换成2015/03/01
            format.setLenient(false);
            format.parse(date);
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }

    /**
     * 初始显示时是否将月份显示全
     *
     * @param monthClear true从1月-12月都显示   false从指定的月份开始显示
     */
    public void setMonthClear(boolean monthClear) {
        this.monthClear = monthClear;
    }

    /**
     * 初始显示时是否将每月的天数显示全
     *
     * @param dayClear true从1号-30（31号）都显示   false从指定的天开始显示
     */
    public void setDayClear(boolean dayClear) {
        this.dayClear = dayClear;
    }

    /**
     * 初始显示时是否将小时数（00-23）显示全
     *
     * @param hourClear
     */
    public void setHourClear(boolean hourClear) {
        this.hourClear = hourClear;
    }

    /**
     * 初始显示时是否将分钟数（00-59）显示全
     *
     * @param minClear
     */
    public void setMinClear(boolean minClear) {
        this.minClear = minClear;
    }

    @OnClick({R.id.confirm, R.id.close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
//                Utils.showToast(this, sdf.format(selectedCalender.getTime()));
                if(type==3){
                    sdf = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
                }
                Intent intent = new Intent();
                intent.putExtra("date", sdf.format(selectedCalender.getTime()));
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.close:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();

        overridePendingTransition(0, R.anim.anim_exit_from_bottom);
    }
}
